//
//  ext.swift
//  XLProject
//
//  Created by xinglian on 2022/1/22.
//

import UIKit
import CommonCrypto

extension NSString
{
    //MARK: - 十进制转换成十六进制
    ///十进制转换成十六进制
    @objc func toHex() ->NSString
    {
        return String(self.intValue,radix:16) as NSString
    }
    
    @objc func size(_ font : UIFont, _ width : CGFloat)->CGSize
    {
        let size = CGSize(width:width, height:CGFloat(MAXFLOAT))
        let rect = self.boundingRect(with: size, options: [.usesFontLeading,.usesLineFragmentOrigin,.truncatesLastVisibleLine], attributes: [NSAttributedString.Key.font:font], context: nil)
        return CGSize(width:ceil(rect.size.width), height:ceil(rect.size.height))
    }
    
    
    
    var md5: String
    {
        let str = self.cString(using: String.Encoding.utf8.rawValue)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8.rawValue))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(str!, strLen, result)
        
        let hash = NSMutableString()
        for i in 0..<digestLen {
            hash.appendFormat("%02x", result[i])
        }
        
        result.deallocate()
        return hash as String
    }
    
}
